import {Result} from '@/base/Result';
import {request} from '@/base/Request';
import {Page} from '@/base/Page';
import {Rule} from '@/base/model/Rule';
import {TableSort} from '@/base/TableSort';
import {isEmpty} from '@/base/util/Strings';
import {Role} from "@/base/model/Role";

export function getRule(params: {
  pageIndex: number,
  pageSize: number,
  sortDirection?:string
}): Promise<Result<Page<Rule>>> {
  return request.getPage(Rule, '/api/member-level/fetchMission', {params});
}
export function putRuleRes(params: {
  res1: Array<string>,
  res2: Array<string>,
}): Promise<Result<Rule>> {
  let formData1 = new FormData();
  const res1 = params.res1.join(',').split(',').filter(x => !isEmpty(x));

  delete params.res1;
  Object.keys(params).forEach(x => {
    formData1.append(x, params[x]);
  });
  if (res1.length > 0) {
    res1.forEach(x => {
      formData1.append('res1', x);
    });
  } else {
    formData1.append('res1', '');
  }

  let formData2 = new FormData();
  const res2 = params.res2.join(',').split(',').filter(x => !isEmpty(x));
  delete params.res2;
  Object.keys(params).forEach(x => {
    formData2.append(x, params[x]);
  });
  if (res2.length > 0) {
    res2.forEach(x => {
      formData2.append('res2', x);
    });
  } else {
    formData2.append('res2', '');
  }


  return request.put(Rule, '/api/role/res', formData1);
}

